
enum CellState {
    UnInit = 0,
    Mask,       //初始状态
    MarkBoom,   //标记为炸弹
    MarkQuote,  //标记为未知
    Open,       //打开
}
enum CellValue {
    Empty = 0,
    Num1 = 1,
    Num2,
    Num3,
    Num4,
    Num5,
    Num6,
    Num7,
    Num8,
    Boom,
}
//Block大小
const MineBlockWidth = 5
const MineBlockHeight = 5

//Cell大小
const MineCellWidth = 50
const MineCellHeight = 50

function PackCellStateAndValue(state: CellState, value: CellValue): number {
    return value + (state * 10)
}
function UnPackCellState(n: number): CellState {
    return Math.floor(n / 10)
}
function UnPackCellValue(n: number): CellValue {
    return n % 10
}

class MineCell extends egret.Bitmap {
    public Value: CellValue
    public State: CellState
    public SetState(state: CellState): boolean {
        let wrong = false
        this.State = state
        switch (this.State) {
            case CellState.UnInit: {
                this.visible = false
                break
            }
            case CellState.MarkQuote: {
                this.visible = true
                this.texture = RES.getRes("cell_mark_quote_png")
                break
            }
            case CellState.Mask: {
                this.visible = true
                this.texture = RES.getRes("cell_init_png")
                break
            }
            case CellState.MarkBoom: {
                this.visible = true
                if (this.Value == CellValue.Boom) {
                    this.texture = RES.getRes("cell_mark_ok_png")
                } else {
                    this.texture = RES.getRes("cell_boom_png")
                    wrong = true//错误
                }
                break
            }
            case CellState.Open: {
                this.visible = true
                if (this.Value == CellValue.Boom) {
                    this.texture = RES.getRes("cell_target_png")
                    wrong = true//错误
                } else if (this.Value == CellValue.Empty) {
                    this.texture = RES.getRes("cell_none_png")
                }
                else {
                    this.texture = RES.getRes("cell_mark_" + this.Value + "_png")
                }
                break
            }
        }

        this.width = MineCellWidth
        this.height = MineCellHeight
        this.anchorOffsetX = this.width / 2
        this.anchorOffsetY = this.height / 2
        return !wrong
    }
    constructor(value: CellValue, state: CellState) {
        super()
        this.Value = value
        this.SetState(CellState.UnInit)
    }
}

class MineBlock {
    public cell: Array<MineCell>
    public map: MineMap
    public startX: number
    public startY: number
    public width: number
    public height: number

    constructor(x, y, h, w: number, map: MineMap) {
        this.startX = x
        this.startY = y
        this.width = w
        this.height = h
        this.cell = new Array<MineCell>(h * w)
        this.map = map
        for (let i = 0; i < this.cell.length; i++) {
            let cell = new MineCell(CellValue.Empty, CellState.Mask)
            cell.x = MineCellWidth * (x + i % this.width)
            cell.y = MineCellHeight * (y + Math.floor(i / this.width))
            this.map.back_ground.addChild(cell)
            this.cell[i] = cell
        }
    }

    public InitBlock(val: Array<number>) {

    }

    public GetCell(x, y: number): CellState {
        if ((x >= this.startX && x < this.startX + this.width) && (y >= this.startY && y < this.startY + this.height)) {
            return this.cell[(x - this.startX) + (y - this.startY) * this.width].State
        }
        return -1
    }

    public SetCell(x, y: number, v: CellState) {
        if ((x >= this.startX && x < this.startX + this.width) && (y >= this.startY && y < this.startY + this.height)) {
            this.cell[(x - this.startX) + (y - this.startY) * this.width].SetState(v)
        }
    }
}

class MineMap {
    public width: number
    public height: number
    public block_width: number
    public block_height: number
    public block_x: number
    public block_y: number
    public block: Array<MineBlock>
    public back_ground: egret.DisplayObjectContainer

    constructor(w, h, bw, bh: number, view: egret.DisplayObjectContainer) {
        this.width = w
        this.height = h
        this.block_width = bw
        this.block_height = bh
        this.block_x = Math.ceil(this.width / this.block_width)
        this.block_y = Math.ceil(this.height / this.block_height)
        this.block = new Array<MineBlock>(this.block_x * this.block_y)
        this.back_ground = view
    }

    public GetBlock(x, y: number): MineBlock {
        if ((x >= 0 && x < this.width) && (y >= 0 && y < this.height)) {
            let index = x + y * this.block_x
            let block = this.block[index]
            if (block == null) {
                block = new MineBlock(Math.floor(x / this.block_width) * this.block_width, Math.floor(y / this.block_height) * this.block_height, this.block_width, this.block_height, this)
                this.block[index] = block
            }
            return block
        }
        return null
    }

    public GetCell(x, y: number): CellState {
        let block = this.GetBlock(x, y)
        if (block != null) {
            return block.GetCell(x, y)
        }
        return -1
    }

    public SetCell(x, y: number, v: CellState) {
        let block = this.GetBlock(x, y)
        if (block != null) {
            block.SetCell(x, y, v)
        }
    }

    public InitBlock(x, y: number, val: Array<CellState>) {
        let block = this.GetBlock(x, y)
        if (block != null) {

        }
    }
}